package com.clsfd.database;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Alert {
	
	private int alertID=0;
	private int LocalalertID=0;
	private Timestamp activatedtime=null;
	private int serviceitem=0;
	private String Servicetype ="";
	private int advertID=0;
	private String MobileOperator="";
	private String Status="";
	private java.sql.Date Expiredate=null;
	private String Smseng="";
	private String Smssin="";
	private String Smstam="";
	private DatabaseConnection MysqlConn=null;
	private Boolean Delivered = false;
	
	public Alert(DatabaseConnection databaseConn,int alertID, int localalertid, int serviceitem,
			 String smseng, String smssin, String smstam, Boolean delivered) {
		MysqlConn=databaseConn;
		this.alertID = alertID;
		this.serviceitem = serviceitem;
		Smseng = smseng;
		Smssin = smssin;
		Smstam = smstam;
		Delivered = delivered;
		LocalalertID=localalertid;
	}

	public Alert(DatabaseConnection databaseConn,int alertID, Timestamp activatedtime, int serviceitem,
			String servicetype, int advertID, String mobileOperator, String status,
			java.sql.Date expiredate, String smseng, String smssin, String smstam) {
		MysqlConn=databaseConn;
		this.alertID = alertID;
		this.activatedtime = activatedtime;
		this.serviceitem = serviceitem;
		this.Servicetype = servicetype;
		this.advertID = advertID;
		MobileOperator = mobileOperator;
		Status = status;
		Expiredate = expiredate;
		Smseng = smseng;
		Smssin = smssin;
		Smstam = smstam;
	}


	public int getAlertID() {
		return alertID;
	}


	public Date getActivatedtime() {
		return activatedtime;
	}

	
	public void setActivatedtime(String activatedtime) throws SQLException, ParseException {
		this.activatedtime = new Timestamp(( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(activatedtime)).getTime());
		MysqlConn.sqlUpdate("update `alerts` set `activatedtime`='"+activatedtime+"' where `alertID`="+alertID);
	}


	public int getServiceitem() {
		return serviceitem;
	}


	public void setServiceitem(int serviceitem) throws SQLException {
		this.serviceitem = serviceitem;		
		MysqlConn.sqlUpdate("update `alerts` set `serviceitem`="+serviceitem+" where `alertID`="+alertID);
	}


	public String getServicetype() {
		return Servicetype;
	}


	public void setServicetype(String servicetype) throws SQLException {
		this.Servicetype = servicetype;
		MysqlConn.sqlUpdate("update `alerts` set `servicetype`='"+servicetype+"' where `alertID`="+alertID);
		
	}


	public int getAdvertID() {
		return advertID;
	}


	public void setAdvertID(int advertID) throws SQLException {
		this.advertID = advertID;
		MysqlConn.sqlUpdate("update `alerts` set `advertid`="+advertID+" where `alertID`="+alertID);
	}


	public String getMobileOperator() {
		return MobileOperator;
	}


	public void setMobileOperator(String mobileOperator) throws SQLException {
		MobileOperator = mobileOperator;
		MysqlConn.sqlUpdate("update `alerts` set `operator`='"+mobileOperator+"' where `alertID`="+alertID);
	}


	public String getStatus() {
		return Status;
	}


	public void setStatus(String status) throws SQLException {
		Status = status;
		MysqlConn.sqlUpdate("update `alerts` set `status`='"+status+"' where `alertID`="+alertID);
	}


	public Date getExpiretedate() {
		return Expiredate;
	}


	public void setExpiretedate(String expiretedate) throws SQLException, ParseException {
		
		this.Expiredate = new java.sql.Date(( new SimpleDateFormat("yyyy-MM-dd").parse(expiretedate)).getTime());
		MysqlConn.sqlUpdate("update `alerts` set `expdate`='"+expiretedate+"' where `alertID`="+alertID);
	}


	public String getSmseng() {
		return Smseng;
	}


	public void setSmseng(String smseng) throws SQLException {
		Smseng = smseng;
		MysqlConn.sqlUpdate("update `alerts` set `smseng`='"+smseng+"' where `alertID`="+alertID);
	}


	public String getSmssin() {
		return Smssin;
	}


	public void setSmssin(String smssin) throws SQLException {
		Smssin = smssin;
		MysqlConn.sqlUpdate("update `alerts` set `smssin`='"+smssin+"' where `alertID`="+alertID);
	}


	public String getSmstam() {
		return Smstam;
	}


	public void setSmstam(String smstam) throws SQLException {
		Smstam = smstam;
		MysqlConn.sqlUpdate("update `alerts` set `smstam`='"+smstam+"' where `alertID`="+alertID);
	}
	
	public void DeleteAlert() throws SQLException {
		MysqlConn.sqlUpdate("delete from `alerts` where `alertID`="+alertID);
		MysqlConn.sqlUpdate("delete from `adverts` where `advertid`="+advertID);
		
	}

	public Boolean getDelivered() {
		return Delivered;
	}

	public void setDelivered(Boolean delivered) throws SQLException {
		Delivered = delivered;
		MysqlConn.sqlUpdate("update `alertstatus` set `delivered`="+(delivered?"1":"0")+" , `senttime`=CURRENT_TIMESTAMP  where `ID`="+LocalalertID);
		
	}

	private int getLocalalertID() {
		return LocalalertID;
	}
	
	
}
